import axios from 'axios'
import { converErrorCodeToMsg,converErrorCodeToMsg2 } from './utils';
import config from '@/config/config';

import find from 'lodash/find';
/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 * @return {object}           An object containing either "data" or "err"
 */
const formDataUrl=[
  {method:'POST',url:'/concentrator_firmwares'},
  {method:'POST',url:'/meter_files'},
  {method:'POST',url:'/mechanical_meter_files'},
  {method:'POST',url:'/gaozhou_meter_files'},
  {method:'POST',url:'/workstations'},
  {method:'PUT',url:'/workstations/'},
  {method:'POST',url:['/meters','images']},
  {method:'POST',url:['/issues','complete']},
  {method:'POST',url:'/reports'},
  {method:'PUT',url:'/reports/'},
  {method:'POST',url:'/water_monthly_reports'},
  {method:'PUT',url:'/water_monthly_reports/'},
  {method:'POST',url:'/articles'},
  {method:'PUT',url:'/articles/'},
  {method:'POST',url:'/audited_documents'},
  {method:'PUT',url:'/audited_documents/'},
  {method:'POST',url:'/leave_lists'},
  {method:'PUT',url:'/leave_lists/'},
  {method:'POST',url:'/cars'},
  {method:'PUT',url:'/cars/'},
  {method:'POST',url:'/seals'},
  {method:'POST',url:'/images'},
  {method:'PUT',url:'/seals/'},
  {method:'POST',url:'/notices'},
  {method:'PUT',url:'/notices/'},
  {method:'POST',url:'/public_files'},
  {method:'PUT',url:'/public_files/'},
  {method:'POST',url:'/project_files'},
]
export default function request(url, options) {
  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  let isInArr=false;
  if(find(formDataUrl,function (item){
    if(Array.isArray(item.url)){
      let tip=false
      for(let i=0;i<item.url.length;i++){
        if(url.indexOf(item.url[i])>=0){
          tip=true
        }else{
          tip=false
        }
      }
      return tip
    }else if(newOptions.method==='PUT'){
      return (item.method===newOptions.method.toUpperCase())&&(url.indexOf(item.url)>=0)
    }else{
      return (item.method===newOptions.method.toUpperCase())&&(item.url===url)
    }
  })){
    console.log('通过formdata表单上传文件')
  }else{
    if(newOptions.method.toUpperCase()==='GET'){
      if(newOptions.params){
        newOptions.params={
          ...newOptions.params,
          company_id:(newOptions.params&&newOptions.params.company_id)?newOptions.params.company_id:sessionStorage.getItem('company_id')
        }
      }else{
        newOptions.params={
          company_id:(newOptions.params&&newOptions.params.company_id)?newOptions.params.company_id:sessionStorage.getItem('company_id')
        }
      }

    }
    if(newOptions.method.toUpperCase()==='POST'||newOptions.method.toUpperCase()==='PUT'){
      if(newOptions.data){
        newOptions.data={
          ...newOptions.data,
          company_id:(newOptions.data&&newOptions.data.company_id)?newOptions.data.company_id:sessionStorage.getItem('company_id')
        }
      }else{
        newOptions.data={
          company_id:(newOptions.data&&newOptions.data.company_id)?newOptions.data.company_id:sessionStorage.getItem('company_id')
        }
      }

    }
  }

  if (url!=='/login') {
    newOptions.headers = {Authorization: `Bearer ${sessionStorage.getItem('token')}`, 'Content-Type': 'application/json;text/plain'}
  }else{
    newOptions.headers = {'Content-Type': 'application/json;text/plain'}

  }

  return axios(`${config.prefix}${url}`, newOptions)
    .then(response => {
      console.log('response',response)
      return response
    })
    .catch((error) => {
      if (url==='/mechanical_meter_files') {
        converErrorCodeToMsg2(error)
      }else{
        converErrorCodeToMsg(error)
      }
      return error;
    });
}
